//=============================================================================
// Mass Driver
//=============================================================================
class TFMassDriver extends TFWeapon
    config(user);

var(Gfx) float testX;
var(Gfx) float testY;

var(Gfx) float borderX;
var(Gfx) float borderY;

var(Gfx) float focusX;
var(Gfx) float focusY;
var(Gfx) float innerArrowsX;
var(Gfx) float innerArrowsY;
var(Gfx) Color ArrowColor;
var(Gfx) Color TargetColor;
var(Gfx) Color NoTargetColor;
var(Gfx) Color FocusColor;
var(Gfx) Color ChargeColor;

var(Gfx) vector RechargeOrigin;
var(Gfx) vector RechargeSize;

var transient float LastFOV;
var() bool zoomed;
var() xEmitter  chargeEmitter;

var Pawn SeekTarget;
var float LockTime, UnLockTime, SeekCheckTime, AmmoCheckTime;
var bool bLockedOn, bBreakLock;
var bool bTightSpread;
var() float SeekCheckFreq, SeekRange;
var() float LockRequiredTime, UnLockRequiredTime;
var() float LockAim;
var() Color CrosshairColor;
var() float CrosshairX, CrosshairY;
var() vector SeekingLocation;
var() float TargetRange, TargetAngle;
var() int TargetHealth, TargetShield;

#exec obj load file=XGameShaders.utx
#exec obj load file=..\Sounds\MenuSounds.uax

replication
{
    reliable if (Role == ROLE_Authority && bNetOwner)
        bLockedOn;

    reliable if (Role == ROLE_Authority && bNetOwner)
        TargetHealth, TargetShield;

    unreliable if (Role == ROLE_Authority)
	zoomed;
}

simulated function PostBeginPlay()
{
    Super.PostBeginPlay();
}

simulated function Destroyed()
{
    if (chargeEmitter != None)
        chargeEmitter.Destroy();

    Super.Destroyed();
}

simulated function ClientWeaponThrown()
{
    if( Instigator != None && Instigator.Controller.IsA( 'PlayerController' ) )
        PlayerController(Instigator.Controller).EndZoom();
    Super.ClientWeaponThrown();
}

function Tick(float dt)
{
    local Pawn Other;
    local Vector StartTrace;
    local Rotator Aim;
    local float BestDist, BestAim;

    if (Instigator == None || Instigator.Weapon != self)
        return;

	if ( Role < ROLE_Authority )
		return;

    if ( !Instigator.IsHumanControlled() )
        return;

    if (Level.TimeSeconds > SeekCheckTime)	//zoomed &&
    {
        if (bBreakLock)
        {
            bBreakLock = false;
            bLockedOn = false;
            SeekTarget = None;
        }

        StartTrace = Instigator.Location + Instigator.EyePosition();
        Aim = Instigator.GetViewRotation();

        BestAim = LockAim;
        Other = Instigator.Controller.PickTarget(BestAim, BestDist, Vector(Aim), StartTrace, SeekRange);

        if ( CanLockOnTo(Other) )
        {
            if (Other == SeekTarget)
            {
                LockTime += SeekCheckFreq;

		TargetRange = VSize(Instigator.Location - Other.Location);
		TargetRange /= 49; //Approximate conversion to metres.
		if (Other.IsA('Pawn'))
		TargetHealth = Other.Health;
		if (Other.IsA('xPawn'))
		TargetShield = xPawn(Other).GetShieldStrength();

                if (!bLockedOn && LockTime >= LockRequiredTime)
                {
                    bLockedOn = true;
		    if (zoomed == true)
                    PlayerController(Instigator.Controller).ClientPlaySound(Sound'MenuSounds.J_MouseOver');
		    SeekingLocation=SeekTarget.Location;
                 }
            }
            else
            {
                SeekTarget = Other;
		if (zoomed == true)
                PlayerController(Instigator.Controller).ClientPlaySound(Sound'MenuSounds.J_MouseOver');
                LockTime = 0.0;
            }
            UnLockTime = 0.0;
        }
        else
        {
            if (SeekTarget != None)
            {
                UnLockTime += SeekCheckFreq;
                if (UnLockTime >= UnLockRequiredTime)
                {
                    SeekTarget = None;
                    if (bLockedOn)
                    {
                        bLockedOn = false;
//                        PlayerController(Instigator.Controller).ClientPlaySound(Sound'MenuSounds.J_MouseOver');
		if (zoomed == true)
                PlayerController(Instigator.Controller).ClientPlaySound(sound'TFMedia.ZoomBeep1');
			SeekingLocation=vect(0,0,0);
                    }
                }
            }
            else
                 bLockedOn = false;
         }

        SeekCheckTime = Level.TimeSeconds + SeekCheckFreq;
    }
}

function bool CanLockOnTo(Actor Other)
{
    local Pawn P;
    P = Pawn(Other);

    if (P == None || P == Instigator || !P.bProjTarget)
        return false;

    if (!Level.Game.bTeamGame)
        return true;

    return ( (P.PlayerReplicationInfo == None) || (P.PlayerReplicationInfo.Team != Instigator.PlayerReplicationInfo.Team) );
}


simulated function SetZoomBlendColor(Canvas c)
{
    local Byte    val;
    local Color   clr;
    local Color   fog;

    clr.R = 255;
    clr.G = 255;
    clr.B = 255;
    clr.A = 255;

    if( Instigator.Region.Zone.bDistanceFog )
    {
        fog = Instigator.Region.Zone.DistanceFogColor;
        val = 0;
        val = Max( val, fog.R);
        val = Max( val, fog.G);
        val = Max( val, fog.B);

        if( val > 128 )
        {
            val -= 128;
            clr.R -= val;
            clr.G -= val;
            clr.B -= val;
        }
    }
    c.DrawColor = clr;
}



simulated event RenderOverlays( Canvas Canvas )
{
	local float tileScaleX;
	local float tileScaleY;
	local float bX;
	local float bY;
	local float fX;
	local float fY;
//	local float charge;

	local float barOrgX;
	local float barOrgY;
	local float barSizeX;
	local float barSizeY;

//        local PainterFire PainterFire;

	local Vector X,Y,Z;
	local Vector HitLocation, HitNormal;

	Ammo[0].CheckOutOfAmmo();

    //    FireSound=Sound'WeaponSounds.LightningGun.LightningScope'
    if ( LastFOV > PlayerController(Instigator.Controller).DesiredFOV )
    {
        PlaySound(Sound'WeaponSounds.LightningGun.LightningZoomIn', SLOT_Misc,,,,,false);
	Canvas.Style = 255;
	Canvas.SetPos(0,0);
	Canvas.DrawColor = class'Canvas'.static.MakeColor(255,255,255);
	Canvas.DrawTile( Material'ScreenNoiseFB', Canvas.SizeX, Canvas.SizeY, 0.0, 0.0, 512, 512 );

    }
    else if ( LastFOV < PlayerController(Instigator.Controller).DesiredFOV )
    {
        PlaySound(Sound'WeaponSounds.LightningGun.LightningZoomOut', SLOT_Misc,,,,,false);
    }
    LastFOV = PlayerController(Instigator.Controller).DesiredFOV;

    if ( (PlayerController(Instigator.Controller).DesiredFOV == PlayerController(Instigator.Controller).DefaultFOV)
		|| (Level.bClassicView && (PlayerController(Instigator.Controller).DesiredFOV == 90)) )
	{
        Super.RenderOverlays(Canvas);
		zoomed=false;
	}
	else
    {

		if ( FireMode[0].NextFireTime <= Level.TimeSeconds )
		{
			charge = 1.0;
		}
		else
		{
			charge = 1.0 - ((FireMode[0].NextFireTime-Level.TimeSeconds) / FireMode[0].FireRate);
		}


		tileScaleX = Canvas.SizeX / 640.0f;
		tileScaleY = Canvas.SizeY / 480.0f;

		bX = borderX * tileScaleX;
		bY = borderY * tileScaleY;
		fX = focusX * tileScaleX;
		fY = focusY * tileScaleX;

		barOrgX = RechargeOrigin.X * tileScaleX;
		barOrgY = RechargeOrigin.Y * tileScaleY;

		barSizeX = RechargeSize.X * tileScaleX;
		barSizeY = RechargeSize.Y * tileScaleY;

        SetZoomBlendColor(Canvas);

        Canvas.Style = 255;
		Canvas.SetPos(0,0);
//      Canvas.DrawTile( Material'ZoomFB', Canvas.SizeX, Canvas.SizeY, 0.0, 0.0, 512, 512 ); // !! hardcoded size
	Canvas.DrawTile( Material'Turrets.TurretHud2', Canvas.SizeX, Canvas.SizeY, 0, 0, 1024, 768 );

		// draw border corners
        Canvas.Style = ERenderStyle.STY_Alpha;
		Canvas.SetPos(0,0);
		Canvas.DrawTile( Texture'SniperBorder', bX, bY, 0.0, 0.0, Texture'SniperBorder'.USize, Texture'SniperBorder'.VSize );

		Canvas.SetPos(Canvas.SizeX-bX,0);
		Canvas.DrawTile( Texture'SniperBorder', bX, bY, 0.0, 0.0, -Texture'SniperBorder'.USize, Texture'SniperBorder'.VSize );

		Canvas.SetPos(Canvas.SizeX-bX,Canvas.SizeY-bY);
		Canvas.DrawTile( Texture'SniperBorder', bX, bY, 0.0, 0.0, -Texture'SniperBorder'.USize, -Texture'SniperBorder'.VSize );

		Canvas.SetPos(0,Canvas.SizeY-bY);
		Canvas.DrawTile( Texture'SniperBorder', bX, bY, 0.0, 0.0, Texture'SniperBorder'.USize, -Texture'SniperBorder'.VSize );


		Canvas.DrawColor = FocusColor;
        Canvas.DrawColor.A = 255; // 255 was the original -asp. WTF??!?!?!
		Canvas.Style = ERenderStyle.STY_Alpha;

		Canvas.SetPos((Canvas.SizeX*0.5)-fX,(Canvas.SizeY*0.5)-fY);
//		Canvas.DrawTile( Texture'SniperFocus', fX*2.0, fY*2.0, 0.0, 0.0, Texture'SniperFocus'.USize, Texture'SniperFocus'.VSize );

        fX = innerArrowsX * tileScaleX;
		fY = innerArrowsY * tileScaleY;

if (bLockedOn == False)
{
		GetViewAxes(X,Y,Z);
		Trace(HitLocation, HitNormal, Instigator.Location + (X * 20001), Instigator.Location, true);
		TargetRange = VSize(Instigator.Location - HitLocation);
		if (TargetRange > 20000)
			TargetRange = -1;
		else
			TargetRange /= 49;

		Canvas.Font = Canvas.MedFont;
		Canvas.SetDrawColor(220,20,20);		//(200,150,0);
		Canvas.DrawColor.A = 240;

		Canvas.SetPos(Canvas.SizeX * 0.77, Canvas.SizeY * 0.46);
		Canvas.DrawText("Target: searching...");
}


if (bLockedOn)
{

        Canvas.DrawColor = ArrowColor;
        Canvas.SetPos((Canvas.SizeX*0.5)-fX,(Canvas.SizeY*0.5)-fY);
	Canvas.DrawTile( Texture'SniperArrows', fX*2.0, fY*2.0, 0.0, 0.0, Texture'SniperArrows'.USize, Texture'SniperArrows'.VSize );

		Canvas.Font = Canvas.MedFont;
		Canvas.SetDrawColor(220,20,20);		//(200,150,0);
		Canvas.DrawColor.A = 240;

		Canvas.SetPos(Canvas.SizeX * 0.77, Canvas.SizeY * 0.46);
		Canvas.DrawText("Target: Acquired");

		Canvas.SetPos(Canvas.SizeX * 0.77, Canvas.SizeY * 0.50);
		Canvas.DrawText("Condition:"@TargetHealth$"%");

		Canvas.SetPos(Canvas.SizeX * 0.77, Canvas.SizeY * 0.54);
		Canvas.DrawText("Shield:"@TargetShield$"%");


	Canvas.Style = 255;
	Canvas.SetPos(0,0);
	Canvas.DrawColor = class'Canvas'.static.MakeColor(255,255,255);
Canvas.DrawTile( Material'XGameShaders.RedeemerReticle', 0.5 * Canvas.SizeX, 0.5 * Canvas.SizeY, 0, 0, 512, 512 );
Canvas.SetPos(0.5*Canvas.SizeX,0);
Canvas.DrawTile( Material'XGameShaders.RedeemerReticle', 0.5 * Canvas.SizeX, 0.5 * Canvas.SizeY, 512, 0, -512, 512 );
Canvas.SetPos(0,0.5*Canvas.SizeY);
Canvas.DrawTile( Material'XGameShaders.RedeemerReticle', 0.5 * Canvas.SizeX, 0.5 * Canvas.SizeY, 0, 512, 512, -512 );
Canvas.SetPos(0.5*Canvas.SizeX,0.5*Canvas.SizeY);
Canvas.DrawTile( Material'XGameShaders.RedeemerReticle', 0.5 * Canvas.SizeX, 0.5 * Canvas.SizeY, 512, 512, -512, -512 );

}

		Canvas.Font = Canvas.MedFont;
		Canvas.SetDrawColor(220,20,20);		//(200,150,0);
		Canvas.DrawColor.A = 240;

		Canvas.SetPos(Canvas.SizeX * 0.10, Canvas.SizeY * 0.46);
		Canvas.DrawText("Zoom :"@PlayerController(Instigator.Controller).DefaultFOV / 							PlayerController(Instigator.Controller).DesiredFOV$"x");

		Canvas.SetPos(Canvas.SizeX * 0.10, Canvas.SizeY * 0.50);
		Canvas.DrawText("Range:"@TargetRange$"m");

		Canvas.SetPos(Canvas.SizeX * 0.10, Canvas.SizeY * 0.54);
		TargetAngle = normalize(Instigator.GetViewRotation()).Pitch / 182.0;
		if (TargetAngle < 0) TargetAngle*= -1;
		Canvas.DrawText("Angle:"@TargetAngle$"");



		Canvas.DrawColor = ChargeColor;
	        Canvas.DrawColor.A = 255;


		if(charge <1)
		    Canvas.DrawColor.R = 255*charge;
		else
        {
            Canvas.DrawColor.R = 0;
		    Canvas.DrawColor.B = 0;
        }

		if(charge == 1)
		    Canvas.DrawColor.G = 255;
		else
		    Canvas.DrawColor.G = 0;

		Canvas.Style = ERenderStyle.STY_Alpha;
		Canvas.SetPos( barOrgX, barOrgY );
		Canvas.DrawTile(Texture'Engine.WhiteTexture',barSizeX,barSizeY*charge, 0.0, 0.0,Texture'Engine.WhiteTexture'.USize,Texture'Engine.WhiteTexture'.VSize*charge);
		zoomed = true;
	}
}


simulated function ClientStartFire(int mode)
{
    if (mode == 1)
    {
        FireMode[mode].bIsFiring = true;
        if( Instigator.Controller.IsA( 'PlayerController' ) )
            PlayerController(Instigator.Controller).ToggleZoom();
    }
    else
    {
        Super.ClientStartFire(mode);
    }
}

simulated function ClientStopFire(int mode)
{
    if (mode == 1)
    {
        FireMode[mode].bIsFiring = false;
        if( Instigator.Controller.IsA( 'PlayerController' ) )
            PlayerController(Instigator.Controller).StopZoom();
    }
    else
    {
        Super.ClientStopFire(mode);
    }
}

simulated function BringUp(optional Weapon PrevWeapon)
{
    if ( PlayerController(Instigator.Controller) != None )
        LastFOV = PlayerController(Instigator.Controller).DesiredFOV;
    Super.BringUp();
}

simulated function bool PutDown()
{
    if( Instigator.Controller.IsA( 'PlayerController' ) )
        PlayerController(Instigator.Controller).EndZoom();
    if ( Super.PutDown() )
    {
		GotoState('');
		return true;
	}
	return false;
}

function float GetAIRating()
{
	return AIRating;
}

function float RangedAttackTime()
{
	return 0;
}

/* BestMode()
choose between regular or alt-fire
*/
function byte BestMode()
{
	return 0;
}


simulated function float ChargeBar()
{
	return FMin((1.0 - ((FireMode[0].NextFireTime-Level.TimeSeconds) / FireMode[0].FireRate)), 0.999);
}

defaultproperties
{
     borderX=60.000000
     borderY=60.000000
     focusX=135.000000
     focusY=105.000000
     innerArrowsX=42.000000
     innerArrowsY=42.000000
     ArrowColor=(B=20,G=20,R=220,A=255)
     FocusColor=(B=71,G=90,R=126,A=215)
     ChargeColor=(G=255,A=200)
     RechargeOrigin=(X=620.000000,Y=330.000000)
     RechargeSize=(X=10.000000,Y=-180.000000)
     SeekCheckFreq=0.250000
     SeekRange=20000.000000
     LockAim=0.996000
     FireModeClass(0)=Class'Taskforces.MassDriverFire'
     FireModeClass(1)=Class'Taskforces.MassDriverZoom'
     SelectAnim="Pickup"
     PutDownAnim="PutDown"
     SelectSound=Sound'WeaponSounds.Misc.redeemer_change'
     SelectForce="SwitchToFlakCannon"
     AIRating=0.900000
     CurrentRating=0.900000
     bShowChargingBar=True
     bNoAmmoInstances=False
     EffectOffset=(X=100.000000,Y=25.000000,Z=-3.000000)
     DisplayFOV=60.000000
     HudColor=(B=128,G=0)
     SmallViewOffset=(X=37.000000,Y=8.000000,Z=-7.000000)
     CenteredOffsetY=-22.000000
     CenteredRoll=1000
     InventoryGroup=9
     GroupOffset=2
     PickupClass=Class'Taskforces.MassDriverPickup'
     PlayerViewOffset=(X=25.000000,Y=2.000000,Z=-1.000000)
     BobDamping=1.575000
     AttachmentClass=Class'Taskforces.MassDriverAttachment'
     IconMaterial=Texture'TFMedia.General.ArkonHUD'
     IconCoords=(Y1=407,X2=118,Y2=442)
     ItemName="Mass Driver"
     LightHue=80
     LightSaturation=70
     LightBrightness=150.000000
     LightRadius=4.000000
     LightPeriod=3
     Mesh=SkeletalMesh'Weapons.Painter_1st'
     DrawScale3D=(Y=1.100000)
     Skins(0)=Shader'TFMedia.MassDriver.MassDriverShader1'
     UV2Texture=Shader'XGameShaders.WeaponShaders.WeaponEnvShader'
     AmmoRegenAmount(0)=2
     SkillCost=50
}
